﻿package mortal.game.view.common.guide.stepOperater
{
    import com.gengine.global.*;
    import com.mui.utils.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.common.swfPlayer.*;
    import mortal.common.swfPlayer.data.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.player.item.*;
    import mortal.game.view.common.guide.data.*;
    import mortal.mvc.core.*;

    public class AutoGuidePickUpItemSteper extends AutoGuideStepBase implements IAutoGuideSteper
    {
        private var _x:int;
        private var _y:int;
        private var _width:int;
        private var _height:int;
        private var _swf:SWFPlayer;
        private var timerId:int;
        private var _guidingCode:int;

        public function AutoGuidePickUpItemSteper()
        {
            return;
        }// end function

        public function guide(param1:AutoGuideData, param2:Window = null) : void
        {
            _data = param1;
            setTimeout(this.guideAction, 50, 14);
            return;
        }// end function

        public function guideAction(param1:int) : void
        {
            var _loc_2:int = 0;
            var _loc_3:ItemInfo = null;
            var _loc_4:ItemPlayer = null;
            if (_working)
            {
                this.stop();
            }
            if (this.timerId > 0)
            {
                clearTimeout(this.timerId);
                this.timerId = -1;
            }
            if (_data.guideTypeValue is int)
            {
                _loc_2 = int(_data.guideTypeValue);
            }
            else
            {
                _loc_2 = this.getItemCode(_data.guideTypeValue as String);
            }
            this._guidingCode = _loc_2;
            if (_loc_2 < 100)
            {
                this._guidingCode = _data.guideTypeValue;
                _loc_4 = ThingUtil.dropItemUtil.getDorpItemByItemUnit(_loc_2);
            }
            else
            {
                _loc_3 = ItemConfig.instance.getConfig(_loc_2);
                _loc_4 = ThingUtil.dropItemUtil.getDorpItemByItemCode(_loc_2);
            }
            if (_loc_4 == null && _loc_3 != null)
            {
                _loc_4 = ThingUtil.dropItemUtil.getDorpItemByItemCode(_loc_3.codeUnbind);
            }
            if (_loc_4 == null)
            {
                if (param1 > 0)
                {
                    this.timerId = setTimeout(this.guideAction, 50, (param1 - 1));
                    return;
                }
                this.stop(_data);
                return;
            }
            if (_data.clickChildName == "rightNow")
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.KeyPickDropItem));
                this.stop(_data);
                return;
            }
            GameController.guide.lastPickoutCode = _loc_2;
            this._width = 36 + 200;
            this._height = 36 + 200;
            this._x = _loc_4.stagePointX - this._width / 2 - 100;
            this._y = _loc_4.stagePointY - this._height / 2 - 100;
            if (this._swf == null)
            {
                this._swf = UICompomentPool.getUICompoment(SWFPlayer) as SWFPlayer;
            }
            this._swf.timeRate = 2;
            this._swf.move(this._x + 235, this._y + 120);
            this._swf.load("guidePickup.swf", ModelType.NormalSwf, null);
            this._swf.play();
            LayerManager.highestLayer.addChild(this._swf);
            if (_data.needMask)
            {
                this.showMask(LayerManager.highestLayer, null);
            }
            if (_data.needArrow)
            {
                showArrowByRect(LayerManager.highestLayer, _data, new Rectangle(this._x, this._y, this._width, this._height));
            }
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsAdd, this.guideTaskRewardUseHandler);
            Global.instance.addEnterSecond(this.guideTaskRewardUseHandler);
            super.setData(_data);
            return;
        }// end function

        private function getItemCode(param1:String) : int
        {
            var _loc_4:String = null;
            var _loc_5:Array = null;
            if (param1.indexOf(",") < 0)
            {
                return parseInt(param1);
            }
            var _loc_2:* = param1.split(",");
            var _loc_3:* = Cache.instance.role.roleEntityInfo.entityInfo.career;
            for each (_loc_4 in _loc_2)
            {
                
                _loc_5 = _loc_4.split("#");
                if (int(_loc_5[1]) == _loc_3)
                {
                    return int(_loc_5[0]);
                }
            }
            return -1;
        }// end function

        override public function stop(param1:AutoGuideData = null) : void
        {
            super.stop(param1);
            if (this._swf != null)
            {
                this._swf.dispose(true);
                this._swf = null;
            }
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsAdd, this.guideTaskRewardUseHandler);
            Global.instance.removeEnterSecond(this.guideTaskRewardUseHandler);
            if (param1 != null)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideStepEnd, param1));
            }
            return;
        }// end function

        private function guideTaskRewardUseHandler(param1:Object = null) : void
        {
            if (this._guidingCode < 100)
            {
                if (ThingUtil.dropItemUtil.getDorpItemByItemUnit(this._guidingCode) != null)
                {
                    return;
                }
            }
            else if (ThingUtil.dropItemUtil.getDorpItemByItemCode(int(this._guidingCode)) != null)
            {
                return;
            }
            this.stop(_data);
            return;
        }// end function

        override protected function showMask(param1:DisplayObjectContainer, param2:DisplayObject) : void
        {
            _maskRect.x = this._x - 1;
            _maskRect.y = this._y - 1;
            _maskRect.width = this._width + 2;
            _maskRect.height = this._height + 2;
            stageResizeHandler(null);
            param1.addChild(mask);
            return;
        }// end function

        override protected function guideTimerOut() : void
        {
            super.guideTimerOut();
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsAdd, this.guideTaskRewardUseHandler);
            Global.instance.removeEnterSecond(this.guideTaskRewardUseHandler);
            Dispatcher.dispatchEvent(new DataEvent(EventName.KeyPickDropItem));
            return;
        }// end function

    }
}
